package com.jandar.spider.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jandar.spider.config.property.SpiderConfig;
import com.jandar.spider.enums.ContentBiddingProvinceEnum;
import com.jandar.spider.mapper.ContentBiddingStatisticsMapper;
import com.jandar.spider.utils.ESUtil;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class ContentBiddingStatisticsService {

    @Autowired
    private ContentBiddingStatisticsMapper contentBiddingStatisticsMapper;
    @Autowired
    private SpiderConfig spiderConfig;
    @Autowired
    ESUtil esUtil;

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    public ResponseEntity<Map<String, Object>> checkData() {
        Map<String, Object> map = new HashMap<>();
        Long max = contentBiddingStatisticsMapper.getMaxPubDateByProvince(ContentBiddingProvinceEnum.ZJ.getName());
        long time = 7 * 24 * 60 * 60 * 1000;
        long now = System.currentTimeMillis();
        String newDatePubDate = simpleDateFormat.format(new Date(max));
//        int count = getCount();
////        int repeatCount = getRepeatCount();
        int ESrepeatCount = esUtil.getESRepeatCount();
        StringBuilder message = new StringBuilder();
//        message.append("数据总量：").append(count).append("条").append(";");
////        message.append("数据库重复数据：").append(repeatCount).append("条").append(";");
//        message.append("ES重复数据：").append(ESrepeatCount).append("条").append(";");
        message.append("最新数据发布日期： ").append(newDatePubDate).append(";");
        if (now - time > max) {
            message.append("当前数据日期过旧");
            map.put("message", message.toString());
            return new ResponseEntity<>(map, HttpStatus.INTERNAL_SERVER_ERROR);
        }
//        if (repeatCount > 0) {
//            message.append("数据库有重复数据");
//            map.put("message", message.toString());
//            return new ResponseEntity<>(map, HttpStatus.INTERNAL_SERVER_ERROR);
//        }
        if (ESrepeatCount >= 100) {
            message.append("ES异常");
            map.put("message", message.toString());
            return new ResponseEntity<>(map, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        map.put("message", message.toString());
        return new ResponseEntity<>(map, HttpStatus.OK);
    }

    public String getMaxPubDateByProvince(String province) {
        Long max = contentBiddingStatisticsMapper.getMaxPubDateByProvince(province);
        return simpleDateFormat.format(new Date(max));
    }

    public Integer getCount() {
        return contentBiddingStatisticsMapper.getCount();
    }

    public Integer getRepeatCount() {
        return contentBiddingStatisticsMapper.getRepeatCount();
    }

    public Integer getCountByProvince(String province) {
        return contentBiddingStatisticsMapper.getCountByProvince(province);
    }


    public void sendEmailByProvince(String province) {
        if (!spiderConfig.getSpider().getSendEmail()) {
            return;
        }
        OkHttpClient client = new OkHttpClient();
        String maxPubDate = getMaxPubDateByProvince(province);
        Integer count = getCountByProvince(province);
        FormBody.Builder form = new FormBody.Builder();
        form.add("subject", province + "数据监控");
        form.add("content", "最新数据发布日期：" + maxPubDate + "<br> \n 数据总量：" + count);
        form.add("to", "陈龙:1838189006@qq.com");
        form.add("fromName", "jandar");

        Request request = new Request.Builder()
                .url("http://172.16.137.55:8081/email-service/sendMail/send")
                .post(form.build())
                .build();

        Response response = null;
        try {
            response = client.newCall(request).execute();
            System.out.println(response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}